/**
 *	 
 */
package edu.gatech.oad.yuno;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * This activity is responsible for creating a new user.
 * @author michael
 *
 */
public class RegisterActivity extends Activity implements View.OnClickListener {
	private Button buttonCreate;
	private Button buttonCancel;
	private EditText editName;
	private EditText editEmail;
	private EditText editUserName;
	private EditText editPassword;
	private EditText editConfirmPassword;

	/**
	 * This method handles the basic functions for this activity - setting up the
	 * fields, buttons, and the listeners.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);

		buttonCreate = (Button) findViewById(R.id.button1);
		buttonCancel = (Button) findViewById(R.id.button2);

		editName = (EditText) findViewById(R.id.name);
		editEmail = (EditText) findViewById(R.id.email);
		editUserName = (EditText) findViewById(R.id.username);
		editPassword = (EditText) findViewById(R.id.password);
		editConfirmPassword = (EditText) findViewById(R.id.confirmpassword);

		buttonCreate.setOnClickListener(this);
		buttonCancel.setOnClickListener(this);
	}

	/**
	 * This method handles the actions for the Create and Cancel buttons
	 */
	public void onClick(View v) {
		if (v == buttonCreate) {
			if (!passwordsMatch()) {
				Toast.makeText(this, "Passwords do not match.",
	    				Toast.LENGTH_SHORT).show();
				return;
			}
			User user = new User();
			user.setUsername(editUserName.getText().toString());
			user.setPassword(editPassword.getText().toString());
			user.setEmail(editEmail.getText().toString());
			user.setName(editName.getText().toString());
			try {
				user.validate();
				user.save();
				finish();
			} catch (ValidationException e) {
				Toast.makeText(this, e.getMessage(),
	    				Toast.LENGTH_SHORT).show();
			}
		} else if (v == buttonCancel) {
			// return the user to the login screen
			finish();
		}
	}
	
	private boolean passwordsMatch(){
		if (editPassword.getText().toString().equals(editConfirmPassword.getText().toString()))
			return true;
		else
			return false;
	}
}
